package com.siloam.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.siloam.module.User;

public class UserDao {
	
	private SqlSession sqlSession;
	
	public SqlSession getSqlSession() {
		return sqlSession;
	}

	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	private String getMapper(String method){
		return "com.siloam.sqlmap.UserMapper."+method;
	}
	
	public void insert(User user){
		sqlSession.insert( getMapper("add"), user);
	}
	
	public void update(User user){
		sqlSession.update("update", user);
	}
	
	public void delete(int id){
		sqlSession.delete("deleteOne", id);
	}
	
	public void delete(String ids){
		sqlSession.delete("deleteSome", ids);
	}
	
	public int getCount(){
		return sqlSession.selectOne("getCount");
	}
	
	public List<User> findPage(int rowBeginIndex,int rowCount){
		Map<String,Integer> param = new HashMap<String, Integer>();
		param.put("rowBeginIndex", rowBeginIndex);
		param.put("rowCount", rowCount);
		return sqlSession.selectList("findPage", param);
	}
	
	public List<User> findAll(){
		return sqlSession.selectList(getMapper("findAll"));
	}
	
	public User load(int id){
		return sqlSession.selectOne( getMapper("load"), id);
	}
	
	public String getMaxUsername(){
		return sqlSession.selectOne("getMaxUserName");
	}
	
	public void setStatus(String ids, int status){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", ids);
		param.put("status", status);
		sqlSession.update( getMapper("setStatus"), param);
	}
	
	public List<User> searchUser(String name){
		return sqlSession.selectList(getMapper("searchUser"), "%"+name+"%");
	}
	
	public List<User> searchUserNoGroup(String name){
		return sqlSession.selectList(getMapper("searchUserNoGroup"), "%"+name+"%");
	}
	
}
